package com.fly.sealedcontainer.util;

import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;

import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 封装一些简单工具方法
 */
public class MyUtil {

    /**
     * 当前工程根目录
     */
    public static final String USER_DIR = System.getProperty("user.dir");

    /**
     * windows文件名非法字符的正则表达式
     */
    public static final String INVALID_FILENAME_REGEX = "[/\\\\:*?<>|\"]";

    /**
     * 获取一个去掉中划线的uuid
     *
     * @return uuid
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 图片下载到本地的路径
     *
     * @param category 图片分类
     * @param suffix   格式后缀
     * @return 绝对路径
     */
    public static String newImagePath(String category, String suffix) {
        return USER_DIR + "/image/" + category + "/" + uuid() + "." + suffix;
    }

    /**
     * 图片下载到本地的路径
     *
     * @param category 图片分类
     * @param filename 文件名
     * @param suffix   格式后缀
     * @return 绝对路径
     */
    public static String newImagePath(String category, String filename, String suffix) {
        filename = filename.replaceAll(INVALID_FILENAME_REGEX,"");
        return USER_DIR + "/image/" + category + "/" + filename + "." + suffix;
    }

    /**
     * 客制化的ObjectMapper
     */
    public static ObjectMapper objectMapper() {
        return JsonMapper.builder()
                .enable(JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER) //开启支持反斜杠
                .build();
    }

    /**
     * 线程池
     */
    public static ExecutorService theadPool() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        return executorService;
    }

    public static void main(String[] args) {
        String url = "https://image.baidu.com/search/acjson?tn=resultjson_com&logid=8987383620925960897&ipn=rj&ct=201326592&is=&fp=result&fr=" +
                "&word=" + "apple" +
                "&queryWord=" + "apple" +
                "&cl=2&lm=&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=&hd=&latest=&copyright=&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&expermode=&nojc=&isAsync=" +
                "&pn=0" + // 开始条数
                "&rn=60" + // 显示数量（最大60）
                "&gsm=3c&1640314046637=";
        System.out.println(url);
    }
}
